#import "MBProgressHUD+Count.h"
#import <stdatomic.h>

static atomic_int _activityCount = 0;

@implementation MBProgressHUD (Count)

+ (void)incrementActivityCount {
    dispatch_async(dispatch_get_main_queue(), ^{
        if (atomic_fetch_add(&_activityCount, 1) == 0) {
            [MBProgressHUD showHUDAddedTo:UIApplication.sharedApplication.keyWindow animated:YES];
        }
    });
}

+ (void)decrementActivityCount {
    dispatch_async(dispatch_get_main_queue(), ^{
        if (atomic_fetch_sub(&_activityCount, 1) <= 1) {
            [MBProgressHUD hideHUDForView:UIApplication.sharedApplication.keyWindow animated:YES];
            atomic_store(&_activityCount, 0);
        }
    });
}

@end